package com.funny.stock.trade.controller;

import com.funny.stock.base.util.ResultUtil;
import com.funny.stock.base.vo.Result;
import com.funny.stock.trade.service.ComposeStockItemService;
import com.funny.stock.trade.service.EmulateComposeService;
import com.funny.stock.trade.vo.EmulateComposeVO;
import com.funny.stock.user.util.UserSecurityUtil;
import com.funny.stock.user.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author pengl
 * @date 2020/05/17
 **/
@RestController
@RequestMapping("/compose")
public class ComposeController {
    public static final Logger logger = LoggerFactory.getLogger(ComposeController.class);

    @Autowired
    EmulateComposeService composeService;

    @Autowired
    ComposeStockItemService composeStockItemService;

//    @Autowired
//    ComposeAssetsService composeAssetsService;


    @PostMapping("/add")
    public Result<EmulateComposeVO> add(@RequestBody EmulateComposeVO emulateComposeVO){
        if(StringUtils.isEmpty(emulateComposeVO.getName()))
            return ResultUtil.error("组合名称不能为空");

        logger.info("add emulateComposeVO");

        SysUserVO user = UserSecurityUtil.getUser();
        emulateComposeVO.setCreateId(user.getId());
        emulateComposeVO.setCreateTime(new Date());
        emulateComposeVO.setUpdateId(user.getId());
        emulateComposeVO.setUpdateTime(new Date());

        EmulateComposeVO queryVO = composeService.findByNameAndCreateId(emulateComposeVO.getName(),user.getId());
        if(queryVO != null){
            return ResultUtil.error("组合名称已存在");
        }

        EmulateComposeVO result = composeService.addCompose(emulateComposeVO);
        return ResultUtil.success(result);
    }

    @GetMapping("/list")
    public Result<List<EmulateComposeVO>> list(){
        List<EmulateComposeVO> myComposeList = composeService.findMyComposeList(UserSecurityUtil.getUserId());

        for (EmulateComposeVO composeVO : myComposeList) {
            composeStockItemService.fillComposeAssetInfo(composeVO);
        }

        return ResultUtil.success(myComposeList);
    }

//    @GetMapping("/listByPage")
//    public Result<Page<EmulateComposeVO>> listByPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                                                     @RequestParam(defaultValue = "20") Integer pageSize){
//
//        Page<EmulateComposeVO> page = composeService.findAll(PageUtil.buildPageRequest(pageNum, pageSize));
//        return ResultUtil.success(page);
//    }

    @GetMapping("/detail/{id}")
    public Result<EmulateComposeVO> detail(@PathVariable Long id){
        EmulateComposeVO detailVO = composeService.findByCreateIdAndId(UserSecurityUtil.getUserId(),id);
        if(detailVO == null || (detailVO.getIsDelete() != null && detailVO.getIsDelete() == 1)){
            return ResultUtil.error("模拟组合不存在");
        }

        //计算资产信息，通过持有的股票现有信息进行计算
        composeStockItemService.fillComposeAssetInfo(detailVO);
        return ResultUtil.success(detailVO);
    }

    @PostMapping("/queryByPage")
    public Result<Page<EmulateComposeVO>> myListByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                       @RequestParam(defaultValue = "20") Integer pageSize,
                                                       @RequestBody EmulateComposeVO emulateComposeVO){

        Long userId = UserSecurityUtil.getUserId();
        emulateComposeVO.setCreateId(userId);

        Page<EmulateComposeVO> page = composeService.queryByCondition(emulateComposeVO,pageNum, pageSize);
        return ResultUtil.success(page);
    }

    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id){
        Long userId = UserSecurityUtil.getUserId();
        EmulateComposeVO queryVO = composeService.findByCreateIdAndId(userId, id);
        if(queryVO != null) {
            queryVO.setIsDelete(1);
            composeService.save(queryVO);
            return ResultUtil.success(null);
        }
        return ResultUtil.error("delete error with id:"+id);
    }
//
//    @PostMapping("/deleteList")
//    public Result<String> deleteList(@RequestBody List<EmulateComposeVO> voList){
//        Long userId = UserSecurityUtil.getUserId();
//
//        Optional<EmulateComposeVO> first = voList.stream().filter(item -> {
//            EmulateComposeVO queryVO = composeService.findByCreateIdAndId(userId, item.getId());
//            return queryVO == null;
//        }).findFirst();
//
//        if(first.isPresent()){
//            return ResultUtil.error("delete error with id:" + first.get().getId());
//        }
//
//        composeService.deleteAll(voList);
//        return ResultUtil.success(null);
//    }
}
